package com.glsc.ngateway.common.base.repo.mysql.cmdb;

import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbPartDetail;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Repository
public interface CmdbPartDetailRepository extends JpaRepository<CmdbPartDetail, Integer>, JpaSpecificationExecutor<CmdbPartDetail> {
    List<CmdbPartDetail> findAllByPartDetailIdIn(List<Long> id);

    @Query(value = "select p from CmdbPartDetail p where p.isDelete = 0 " +
            "and (:#{#paramSearchDto.partStandardCode} is null OR p.partStandardCode LIKE CONCAT('%', :#{#paramSearchDto.partStandardCode}, '%')) " +
            "and (:#{#paramSearchDto.sn} is null OR p.sn LIKE CONCAT('%', :#{#paramSearchDto.sn}, '%')) " +
            "and (:#{#paramSearchDto.manufactDate} is null OR p.manufactDate = :#{#paramSearchDto.manufactDate}) " +
            "and (:#{#paramSearchDto.expirateDate} is null OR p.expirateDate = :#{#paramSearchDto.expirateDate}) " +
            "and (:#{#paramSearchDto.deviceCode} is null OR p.deviceCode = :#{#paramSearchDto.deviceCode}) " +
            "and (:#{#paramSearchDto.locationId} is null OR p.locationId = :#{#paramSearchDto.locationId}) " +
            "and (:#{#paramSearchDto.lifeStatus} is null OR p.lifeStatus = :#{#paramSearchDto.lifeStatus}) " +
            "and (:#{#paramSearchDto.partDetailCode} is null OR p.partDetailCode = :#{#paramSearchDto.partDetailCode}) ")
    Page<CmdbPartDetail> searchByParam(Pageable pageable, CmdbPartDetail paramSearchDto);

    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_part_detail set is_delete = :deleted where part_standard_code in :codes ", nativeQuery = true)
    void updateIsDeleteByCodes(Integer deleted, List<String> codes);

    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_part_detail set is_delete = :deleted where location_id = :deviceDetailId and life_status = 1", nativeQuery = true)
    void updateIsDeleteByDeviceDetailId(Integer deleted, Integer deviceDetailId);

    List<CmdbPartDetail> findAllBySnAndIsDelete(String sn, Integer noDelete);

    CmdbPartDetail findByPartDetailId(Integer partDetailId);

    List<CmdbPartDetail> findAllByPartDetailCodeIn(List<String> collect);

    List<CmdbPartDetail> findAllByDeviceCodeAndIsDelete(String deviceCode, Integer noDeleted);

    List<CmdbPartDetail> findAllBySnIn(List<String> strings);

}

